<?php

	function getMissionChance($chance)
	{
		$cid = $_SESSION['cid'];

		$weaponbonus = getWeaponBonus($cid);
		$weaponbonus = round(($chance/100)*$weaponbonus);

		$armourbonus = getArmourBonus($cid);
		$armourbonus = round(($chance/100)*$armourbonus);

		$chance = $chance+(round(($chance/100)*$level))+$weaponbonus+$armourbonus;

		if($chance < 100)
		{
			return $chance;	
		}
		else
		{
			return 100;
		}
		
	}


	$cid = $_SESSION['cid'];
	$getmission = mysql_query("SELECT * FROM mission_log WHERE cid='$cid' AND finished='0'")or die(mysql_error());

	if(mysql_num_rows($getmission) != 0)
	{
		$f = mysql_fetch_array($getmission);
		$timeleft = $f['endtime']-time();
		if($timeleft <= 0)
		{
			

			$q = mysql_query("SELECT * FROM missions WHERE id='".$f['mid']."'");
			$f2 = mysql_fetch_array($q);

			$chance = getMissionChance($f2['chance']);
			$level = getCharLevel(getAidFromCid($cid));
			
			

			$random = rand(1, 100);


			$aid = $_SESSION['uid'];
			$pollars = getPollars($aid);

			$xp = getCharCurrXp($aid);

			$gettotal = mysql_query("SELECT * FROM mission_log WHERE cid='$cid' AND finished='1'");

			if($random <= $chance)
			{
				logTransaction(6, $f2['pollar_award'], transactionType(6));
				mysql_query("UPDATE mission_log SET finished='1', failsuccess='1' WHERE cid='".$f['cid']."' AND missionnumber='".$f['missionnumber']."'");

				$pollars = $pollars+$f2['pollar_award'];
				$xp = $xp+$f2['xp_award'];

				mysql_query("UPDATE account SET pollars='$pollars' WHERE id='$aid'");
			 	mysql_query("UPDATE characters SET xp='$xp', finished_missions=finished_missions+1 WHERE id='$cid'");

				alert("You finished a mission, ".$f2['pollar_award']." and ".$f2['xp_award']." xp have been added!");
				redirect(getUrl());
			}
			else
			{
				mysql_query("UPDATE mission_log SET finished='1', failsuccess='0' WHERE cid='".$f['cid']."' AND missionnumber='".$f['missionnumber']."'");

				$random = rand(1, 100);

				$runaway = $f2['runawaychance'];
				$level = getCharLevel($aid);
				$level10 = ($level/10)+1;

				$chance = ceil($runaway*$level10);

				$break = '\n';

				if($random <= $chance)
				{
					if($f2['pollar_award'] < 10000)
					{
						$pollarreward = round($f2['pollar_award']/20);	
					}
					else
					{
						$pollarreward = round(4500);
					}
					
					$pollars = $pollars+$pollarreward;
					
					if($f2['xp_award'] < 5000)
					{
						$xpreward = round($f2['xp_award']/20);	
					}
					else
					{
						$pollarreward = round(2200);
					}

					$xp = $xp+$xpreward;

					mysql_query("UPDATE account SET pollars='$pollars' WHERE id='$aid'");
			 		mysql_query("UPDATE characters SET xp='$xp' WHERE id='$cid'");

					alert("You failed the mission, but you succesfully ran away from the cops! $break You got 10% of the reward. (Pollars: $pollarreward, XP: $xpreward).");
					logTransaction(7, $pollarreward, TransactionType(7));
				}
				else
				{
					$q3 = mysql_query("SELECT * FROM jails WHERE minlevel <= '$level' ORDER BY RAND() LIMIT 1")or die(alert(mysql_error()));
					$f3 = mysql_fetch_array($q3);

					$jailname = $f3['jailname'];
					$minutes = $f3['min-minutes'];
					$jid = $f3['id'];

					setJail($jid, $minutes);

					alert("You failed the mission and you got caught by the cops. $break You have to sit in $jailname for $minutes minutes!");
				}

				
			}

		}
	}

	function setMission($id)
	{	
		$q = mysql_query("SELECT * FROM missions WHERE id='$id'");
		$f = mysql_fetch_array($q);

		$minutes = $f['minutes'];
		$energy = $f['energy_costs'];
		$hp = $f['hp_costs'];
		$level = $f['min_level'];

		$time = $minutes*60;
		$time = time()+$time;

		$cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];

		$currEnergy = getCharCurrEnergy($aid);
		$currHp = getCharCurrHp($aid);
		$getLevel = getLevel($aid);

		$getamount = mysql_query("SELECT * FROM mission_log WHERE cid='$cid'");
		$missionnumber = mysql_num_rows($getamount);
		$missionnumber = $missionnumber+1;


		if($currEnergy >= $energy && $currHp >= $hp && $getLevel >= $level)
		{
			if(mysql_query("INSERT INTO mission_log (cid, mid, endtime, missionnumber) VALUES ('$cid', '$id', '$time', '$missionnumber')"))
			{
				RemAddEnergy($energy, 'rem');
				redirect("?page=missions");
			}
		}
		else
		{
			if($currEnergy < $energy)
			{
				alert("Not enough energy!");
				redirect("?page=missions");
			}
			if($currHp < $hp)
			{
				alert("Not enough HP!");
				redirect("?page=missions");
			}
			if($getLevel < $level)
			{
				alert("Not enough HP!");
				redirect("?page=missions");
			}
		}

	}

	function getCurrentMission()
	{
		$cid = $_SESSION['cid'];
		$q = mysql_query("SELECT * FROM mission_log WHERE cid='$cid' AND endtime > '".time()."' AND finished='0'");
		$f = mysql_fetch_array($q);
		
		$endtime = $f['endtime'];


		if(mysql_num_rows($q) >= 1)
		{
			$q = mysql_query("SELECT * FROM missions WHERE id='".$f['mid']."'");
			$f = mysql_fetch_array($q);
			echo '<table>
					<tr><td colspan="4"><b>Current Mission:</b></td></tr>
					<tr>
						<td width="300"><b>description</b></td>
						<td width="80"><b>Pollar</b></td>
						<td width="80"><b>XP</b></td>
						<td width="120"><b>Time left</b></td>
					</tr>
					<tr>
						<td>'.$f['description'].'</td>
						<td>'.$f['pollar_award'].'</td>
						<td>'.$f['xp_award'].'</td>
						<td><div id="starttime" style="display:none;">'.$endtime.'</div><div id="timeleft"></div></td>
					</tr>
				';
		}
		else
		{
			echo '<table>
					<tr><td colspan="4"><b>Current Mission:</b></td></tr>
					<tr><td colspan="4"><b>- No current mission.</b></td></tr>
				';
		}
	}

	function getMissions()
	{
		$aid = $_SESSION['uid'];
		$q = mysql_query("SELECT * FROM missions ORDER BY chance DESC")or die(mysql_error());

		echo '<table style="margin-top:30px;">';

		echo '<tr><td colspan="6"><b>Available Missions:</b></td></tr>';
		echo '<tr>
				<td width="210"><b>description</b></tan(arg)d>
				<td width="90"><b>Req Level</b></td>
				<td width="80"><b>Minutes</b></td>
				<td width="60"><b>Pollar</b></td>
				<td width="60"><b>XP</b></td>
				<td width="70"><b>chance</b></td>
				<td width="50"><b>energy</b></td>
				<td></td>
			</tr>';

		$cid = $_SESSION['cid'];
			
		while($f = mysql_fetch_array($q))
		{
			if(getCharLevel($aid) >= $f['min-level'])
			{
				$getmission = mysql_query("SELECT * FROM mission_log WHERE cid='$cid' AND mid = '".$f['id']."' AND finished='1' ORDER BY missionnumber DESC LIMIT 1")or die(mysql_error());
				$getstat = mysql_fetch_array($getmission);
				if($f['repeatable'] == 0 && $getstat['failsuccess'] == 0 || $f['repeatable'] == 1)
				{

					echo '<tr>
							<td>'.$f['description'].'</td>
							<td>level '.$f['min-level'].'</td>
							<td>'.$f['minutes'].' min.</td>
							<td>'.$f['pollar_award'].'</td>
							<td>'.$f['xp_award'].'</td>
							<td>'.getMissionChance($f['chance']).'%</td>
							<td>'.$f['energy_costs'].'</td>';
						
							$getmission = mysql_query("SELECT * FROM mission_log WHERE cid='$cid' AND endtime > '".time()."' AND finished='0'")or die(mysql_error());
							if(mysql_num_rows($getmission) >= 1)
							{
								echo '<td>Wait..</td>';
							}
							else
							{
								echo '<td><a href="?page=missions&do='.$f['id'].'"><button class="bsexy">Go!</button></a></td>';
							}
					
					echo '</tr>';
				}
			}
		}

		echo '</table>';

		if(isset($_GET['do']))
		{
			setMission($_GET['do'], $_GET['time'], $_GET['energy']);
		}
	}

	function getFinishedMissions()
	{
		$cid = $_SESSION['cid'];

		echo '<table style="margin-top:30px;">';

		echo '<tr><td colspan="6"><b>Last 15 missions:</b></td></tr>';

		$i = 1;
		$q = mysql_query("SELECT * FROM missions, mission_log WHERE missions.id = mission_log.mid AND mission_log.cid = '$cid' AND mission_log.finished='1' ORDER BY mission_log.missionnumber DESC LIMIT 15");
		if(mysql_num_rows($q) >= 1)
		{
			echo '<tr>
					<td width="20">#</td>
					<td width="180"><b>description</b></tan(arg)d>
					<td width="90"><b>Req Level</b></td>
					<td width="80"><b>Minutes</b></td>
					<td width="60"><b>Pollar</b></td>
					<td width="60"><b>XP</b></td>
					<td width="70"><b>chance</b></td>
					<td width="60"><b>energy</b></td>
					<td><b>Result</b></td>
				</tr>';
			
			while($f = mysql_fetch_array($q))
			{
				if($f['failsuccess'] == 1)
				{
					$class = 'bold';
					$finished = 'finished';
				}
				else
				{
					$class = '';
					$finished = 'failed';
				}

				echo '<tr class="'.$class.'">
				 	<td width="20">'.$i.'</td>
					<td width="180">'.$f['description'].'</td>
					<td width="90">'.$f['min-level'].'</td>
					<td width="80">'.$f['minutes'].'</td>
					<td width="60">'.$f['pollar_award'].'</td>
					<td width="60">'.$f['xp_award'].'</td>
					<td width="70">'.getMissionChance($f['chance']).'%</td>
					<td width="50">'.$f['energy_costs'].'</td>
					<td width="50">'.$finished.'</td>
				</tr>';
				$i++;
			}
		}
		else
		{
			echo '<tr>
				 	<td colspan="8">- There are no finished missions yet.</td>
				</tr>';
		}
		
	}
?>